<?php
    class Admincp_CartController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Cart();
            $this->view->headTitle('Quản lý đơn hàng');
        }
        
        public function preDispatch()
        {
            $auth = Zend_Auth::getInstance();
            if(!$auth->hasIdentity()) {
                if($this->_request->getActionName() != 'login')
                    $this->_redirect('admincp/index/login');
            }
        }
        
        public function indexAction()
        {
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listItem());
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
            
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiItem($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
        }
        
        public function showAction()
        {
            $this->_helper->layout->setLayout('popup');
            $id = (int) $this->_arrParam['id'];
            $this->view->cart = $this->_model->getItem($id);
            $this->view->data = $this->_model->getCartInfo($id);
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->_model->deleteItem($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
		    $status = (int) $this->_arrParam['status'];
		    $status = ($status == 1) ? 2 : 1;
		    $data = array(
                'id' => $id,
		        'status' => $status,
		    );
		    $this->_model->saveItem($data, 'update');
		    $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function searchAction()
		{
		    $strFlex = new Zendvn_Filter_RemoveCircumflex();
		    $fullname = strtolower(trim($strFlex->filter(strip_tags($this->_arrParam['fullname']))));
            $address = strtolower(trim($strFlex->filter(strip_tags($this->_arrParam['address']))));
            $email = strtolower(trim($strFlex->filter(strip_tags($this->_arrParam['email']))));
            $phone = trim(strip_tags($this->_arrParam['phone']));
            $start_date = trim(strip_tags($this->_arrParam['start_date']));
		    $end_date = trim(strip_tags($this->_arrParam['end_date']));
            $status = (int) $this->_arrParam['status'];
		    $this->view->keyword = array(
	            'fullname' => $this->_arrParam['fullname'],
                'address' => $this->_arrParam['address'],
                'email' => $this->_arrParam['email'],
                'phone' => $this->_arrParam['phone'],
	            'start_date' => $this->_arrParam['start_date'],
	            'end_date' => $this->_arrParam['end_date'],
                'status' => $this->_arrParam['status'],
		    );
		    $data = array(
	            'fullname' => (string) '%'.str_replace(' ','%',$fullname).'%',
                'address' => (string) '%'.str_replace(' ','%',$address).'%',
                'email' => (string) '%'.str_replace(' ','%',$email).'%',
                'phone' => (string) '%'.str_replace(' ','%',$phone).'%',
	            'start_date' => $start_date,
	            'end_date' => $end_date,
	            'status' => $status,
		    );
		    $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchItem($data));
		    $paginator = new Zend_Paginator($adapter);
		    $paginator->setItemCountPerPage(1);
		    $paginator->setPageRange(5);
		    $currentPage = $this->_request->getParam('page', 1);
		    $paginator->setCurrentPageNumber($currentPage);
		    $this->view->data = $paginator;
		    $this->view->total = $paginator->getTotalItemCount();
		}
    }
